<?php

/**
 * 语音处理Module类
 * Class voiceModule
 * 在做消息互动升级时加入
 * @author lilei<LL_strive@163.com> 2017-07-28
 */
class voiceModule extends Module {

    // Module::doModule()方法调用
    public function run($message, $material, $userStatus) {
        $materialObj = simplexml_load_string($material->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
//        $rm = $message->reply(message_type_voice);
        $rm = new stdClass();
        $rm->content        = '';
        $rm->fromUserName   = $message->toUserName;
        $rm->toUserName     = $message->fromUserName;
        $rm->createTime     = time();
        $rm->msgType        = message_type_voice;
        $rm->funcFlag       = 0;

        if ($material->media_id) {  // 如果语音已经上传过微信, 会有media_id
            $rm->media_id = (String)$material->media_id;
        } else {    // 如果尚未上传过微信, 则需要从oss下载语音资源, 然后上传

            // 先看一下现在公众号后台素材的数量
            Module::callModule("messageSearch", null, "getMaterialCount", NULL);

            // 然后实例化咱们的素材数据表, 目的为了在素材内容上传后保存素材的media_id
            $materialModel = D("material");
            $id = $material->id;

            // 拿到xml数据里有用数据内容
            $materialObj = $materialObj->voice->item;

            // 将资源从OSS下载到本地
            $result = self::saveFile((String)$materialObj->voiceUrl, message_type_voice);
            $file_info = array(
                'filename' => $result['path'], // 资源相对于网站根目录的路径
                'content-type' => $result['content-type'], // 资源类型
                'filelength' => $result['size'] // 资源大小
            );
            $path = $result['path'];

            // 资源上传微信素材库
            $result = WeixinApi::add_material("voice", $file_info, $message->indMediaId);
            @unlink($path);     // 释放本地资源
            $result = json_decode($result, true);
            $materialModel->where(['id' => $id])->setField("media_id", $result['media_id']);
            $rm->media_id = $result['media_id'];
        }

        // 为用户打上访问标签
        if (!empty($material->accessTag) && !empty($message->fromUserName)) {
            setAccessTags($material->accessTag, $message->fromUserName, module_handle_text, $message->id);
        }

        // 返回
        return $rm;
    }

    /**
     * 将语音资源下载到本地
     *
     * @param $path     // OSS地址
     * @param $type     // 类型: voice
     * @return mixed
     */
    public function saveFile($path, $type)
    {
        $fileName = pathinfo($path);
        $voice_name = strToLower($fileName['basename']);

        $ch = curl_init($path);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $voice = curl_exec($ch);
        curl_close($ch);

        $fp = fopen($voice_name, 'w');
        fwrite($fp, $voice);
        fclose($fp);

        $result['size'] = filesize($voice_name);
        $result['path'] = $voice_name;
        $result['content-type'] = $type . "/" . $fileName['extension'];
        return $result;
    }

}

$module = new voiceModule();
Module::addModule($module);